//
//  TwelveViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/3/10.
//  Copyright © 2020 航电. All rights reserved.
//
/*
 实现下拉放大效果：
 1.表头图片让它自适应填充scaleAspectFill，然后通过滚动代理scrollViewDidScroll，根据位移量改变图片的高度
 2.表头图片不填充的话，滚动的时候改变图片的x、width和height的大小
 */

import UIKit

let IS_IPHONE_X = ((screenMainSize.height >= 812) ? true : false)
let NAVIGATION_HEIGHT:CGFloat = (IS_IPHONE_X ? 88 : 64)

class TwelveViewController: UIViewController {
    
    fileprivate let cellId = "twelve_cell";
    fileprivate lazy var mainTableView:UITableView = {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenMainSize.width, height: screenMainSize.height), style: UITableView.Style.plain);
        tableView.backgroundColor = .white;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellId);
        tableView.tableHeaderView = self.headerView;
        return tableView;
    }();
    
    fileprivate lazy var headerView:UIImageView = {
        let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: screenMainSize.width, height: 200));
        imgView.image = UIImage(named: "onepiece");
        imgView.contentMode = .scaleAspectFill;
        return imgView;
    }();
    
    fileprivate lazy var dataArr:NSMutableArray = {
        var dataArr = NSMutableArray();
        for index in 0...100 {
            dataArr.add("\(index+1)");
        }
        return dataArr;
    }();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "下拉背景放大"
        // Do any additional setup after loading the view.
        self.onCreateUIForNavigation();
        self.onCreateUIForView();
    }
    
    //TODO:UI
    fileprivate func onCreateUIForNavigation() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: UIBarButtonItem.Style.done, target: self, action: #selector(onBackAction))
    }
    
    fileprivate func onCreateUIForView() {
        self.view.backgroundColor = .white;
        
        self.view.addSubview(self.mainTableView);
    }

    //TODO:Actions
    @objc fileprivate func onBackAction() {
        self.dismiss(animated: true, completion: nil);
    }
}

extension TwelveViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId);
        cell?.backgroundColor = .lightGray;
        
        if self.dataArr.count > indexPath.row {
            cell?.textLabel?.text = (self.dataArr[indexPath.row] as! String);
            cell?.textLabel?.textAlignment = .center;
        }
        
        return cell!;
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("偏移量:\(scrollView.contentOffset.y)")
        let offsetY = scrollView.contentOffset.y;
        let contentY:Float = fabsf(Float(scrollView.contentOffset.y));
        if offsetY >= 0 {
            //上拉
            self.navigationController?.navigationBar.alpha = 0;
        }
        else if offsetY > -(NAVIGATION_HEIGHT) {
            //上拉
            self.navigationController?.navigationBar.alpha = abs(offsetY)/88;
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red];
            
            self.headerView.frame = CGRect(x: 0, y: 0, width: screenMainSize.width, height: 200-(NAVIGATION_HEIGHT-CGFloat(contentY)));
        } else {
            //下拉
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.black];
            if contentY > Float(NAVIGATION_HEIGHT) {
                
                self.headerView.frame = CGRect(x: 0, y: 0, width: screenMainSize.width, height: 200+CGFloat(contentY)-NAVIGATION_HEIGHT);
            }
        }
        
        self.mainTableView.tableHeaderView = self.headerView;
    }
}
